<template>
  <a-layout>
    <a-layout-content>
      <div class="preview">
        <a-form-model :model="formTitle" ref="formTitleRef">
          <h1>{{formTitle.title}}</h1>
          <h3>{{formTitle.intro}}</h3>
          <a-card class="pre-card" v-for="(item,index) in formTitle.options" :key="index">
            <div v-if="item.type=='radio'">
              <h3>
                <span class="isRequired" v-if="item.isRequired">*</span>
                {{index+1}}.{{item.question}}
              </h3>
              <h6 v-if="item.description">说明:{{item.description}}</h6>

              <a-form-model-item
                :prop="'options.'+index+'.radioValue'"
                :rules="{
                required: true, message: '请选择您的答案', trigger: 'blur'
              }"
              >
                <a-radio-group v-model="item.radioValue">
                  <div v-for="(option,opIndex) of item.options" :key="opIndex">
                    <a-radio :value="option.id">{{option.content}}</a-radio>
                  </div>
                </a-radio-group>
              </a-form-model-item>
            </div>
            <div v-if="item.type=='checkbox'">
              <h3>
                <span class="isRequired" v-if="item.isRequired">*</span>
                {{index+1}}.{{item.question}}
              </h3>
              <h6 v-if="item.description">说明:{{item.description}}</h6>
              <a-form-model-item
                :prop="'options.'+index+'.checkboxValue'"
                :rules="{
                required: true, message: '请选择您的答案', trigger: 'blur'
              }"
              >
                <a-checkbox-group v-model="item.checkboxValue">
                  <div v-for="(option,opIndex) of item.options" :key="opIndex">
                    <a-checkbox :value="option.id">{{option.content}}</a-checkbox>
                  </div>
                </a-checkbox-group>
              </a-form-model-item>
            </div>
            <div v-if="item.type=='text'">
              <h3>
                <span class="isRequired" v-if="item.isRequired">*</span>
                {{index+1}}.{{item.question}}
              </h3>
              <h6 v-if="item.description">说明:{{item.description}}</h6>

              <a-form-model-item
                :prop="'options.'+index+'.textValue'"
                :rules="{
                required: true, message: '请选择您的答案', trigger: 'blur'
              }"
              >
                <div v-for="(option,opIndex) of item.options" :key="opIndex">
                  <a-textarea v-model="item.textValue">{{option.content}}</a-textarea>
                </div>
              </a-form-model-item>
            </div>
            <div v-if="item.type=='dropbox'">
              <h3>
                <span class="isRequired" v-if="item.isRequired">*</span>
                {{index+1}}.{{item.question}}
              </h3>
              <h6 v-if="item.description">说明:{{item.description}}</h6>

              <a-form-model-item
                :prop="'options.'+index+'.radioValue'"
                :rules="{
                required: true, message: '请选择您的答案', trigger: 'blur'
              }"
              >
                <a-select label-in-value style="width: 120px" v-model="item.radioValue">
                  <a-select-option
                    v-for="(option,opIndex) of item.options"
                    :key="opIndex"
                    :value="option.id"
                  >{{option.content}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </div>
            <div v-if="item.type=='uploadFile'">
              <h3>
                <span class="isRequired" v-if="item.isRequired">*</span>
                {{index+1}}.{{item.question}}
              </h3>
              <h6 v-if="item.description">说明:{{item.description}}</h6>

              <a-form-model-item
                :prop="'options.'+index+'.radioValue'"
                :rules="{
                required: true, message: '请选择您的答案', trigger: 'blur'
              }"
              >
                <a-upload-dragger
                  class="uploadFile"
                  name="file"
                  :multiple="true"
                  action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
                  @change="handleChange"
                >
                  <p class="ant-upload-drag-icon">
                    <a-icon type="inbox" />
                  </p>
                  <p class="ant-upload-text">请选择文件</p>
                </a-upload-dragger>
              </a-form-model-item>
            </div>
            <div v-if="item.type=='uploadImage'">
              <h3>
                <span class="isRequired" v-if="item.isRequired">*</span>
                {{index+1}}.{{item.question}}
              </h3>
              <h6 v-if="item.description">说明:{{item.description}}</h6>

              <a-form-model-item
                :prop="'options.'+index+'.radioValue'"
                :rules="{
                required: true, message: '请选择您的答案', trigger: 'blur'
              }"
              >
                <a-upload
                  name="avatar"
                  list-type="picture-card"
                  class="avatar-uploader"
                  :show-upload-list="false"
                  action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
                  :before-upload="beforeUpload"
                  @change="handleChange"
                >
                  <img v-if="imageUrl" :src="imageUrl" alt="avatar" />
                  <div v-else>
                    <a-icon :type="loading ? 'loading' : 'plus'" />
                    <div class="ant-upload-text">选择图片</div>
                  </div>
                </a-upload>
              </a-form-model-item>
            </div>
            <!-- 自增表单 -->
            <div v-if="item.type=='increaseForm'">
              <h3>
                <span class="isRequired" v-if="item.isRequired">*</span>
                {{index+1}}.{{item.question}}
              </h3>
              <h6 v-if="item.description">说明:{{item.description}}</h6>

              <a-form-model-item
                :prop="'options.'+index+'.radioValue'"
                :rules="{
                required: true, message: '请选择您的答案', trigger: 'blur'
              }"
              >
                <div class="increaseForm" v-for="(test,index) in increaseForm" :key="index">
                  <div v-for="(option,opIndex) of item.options" :key="opIndex">
                    <div v-if="increaseForm.length <=1">{{option.content}}</div>
                    <a-input v-if="option.type==='text'" :value="option.content"></a-input>

                    <div>
                      <a-select label-in-value style="width: 120px" v-if="option.type==='dropbox'">
                        <a-select-option
                          v-for="(increaseOption,index) of option.increaseOptions"
                          :key="index"
                          :value="increaseOption"
                        >{{increaseOption}}</a-select-option>
                      </a-select>
                    </div>
                  </div>

                  <a-button v-if="increaseForm.length >1" type="danger" @click="minus(index)">-</a-button>
                </div>
              </a-form-model-item>
              <a-button class="increaseButton" @click="plus">
                <a-icon type="plus-circle" />增加列表
              </a-button>
            </div>
          </a-card>
        </a-form-model>
        <a-button @click="submit" type="primary">提交</a-button>
        <a-button @click="makeQrcode">分享</a-button>
        <p class="cardBottom">宝能问卷 提供技术支持</p>
        <canvas id="canvas" style="width: 150px;height: 150px;"></canvas>
      </div>
    </a-layout-content>
  </a-layout>
</template>

<script>
import QRCode from "qrcode";
export default {
  data() {
    return {
      increaseForm: [1],
      formTitle: ""
    };
  },
  created() {
    console.log(window.sessionStorage.getItem("formTitle"));
    let formTitle = JSON.parse(window.sessionStorage.getItem("formTitle"));
    this.formTitle = formTitle;
    console.log(formTitle);
  },
  methods: {
    minus(index) {
      this.increaseForm.splice(index, 1);
    },
    plus() {
      this.increaseForm.push(1);
    },
    submit() {
      this.$refs.formTitleRef.validate(val => {
        console.log(val);
        val && console.log(this.formTitle);
      });

      //   this.$router.push("/thank");
    },
    //生成二维码
    makeQrcode() {
      var canvas = document.getElementById("canvas");
      QRCode.toCanvas(canvas, "https://www.baidu.com");
    }
  },
  beforeRouteEnter(to, from, next) {
    document.body.style.background = "";

    next();
  }
};
</script>

<style lang="scss" scoped>
.ant-layout {
  width: 100%;
  background: #4583fe url("../assets/images/login-plate.png") repeat-y fixed top;
}
.preview {
  width: 1000px;

  margin: 100px auto;
  padding: 0 20px;
  background-color: white;
  text-align: center;
  .isRequired {
    color: red;
  }
  h1 {
    font-size: 30px;
    font-weight: inherit;
    margin-bottom: 20px;
    padding-top: 40px;
  }
  h3 {
    font-size: 18px;
  }
  .pre-card {
    margin: 20px auto;
    box-shadow: 10px 10px 5px #888888;
    text-align: start;
    h6 {
      font-size: 12px;
      color: grey;
      margin-left: 10px;
    }
  }
  .cardBottom {
    margin-top: 100px;
  }
  .increaseForm {
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
  }
  .increaseButton {
    display: block;
    margin: 0 auto;
  }
}
</style>
<style >
</style>